library(tidyverse)
library(janitor)
library(haven)
library(plotly)
library(readr)
library(rjson)
##Creating state level brfss map of asthma prevalence
#Loading the main brfss data from local library
state_df <- read_csv("brfss_data/brfss_12072021.csv")
#Have to use state abbreviations to work with the plotly map
state_df <- state_df %>%
mutate(state = ifelse(state == 66, NA, state)) %>%
drop_na() %>%
mutate(state = factor(state, labels = c("AL", 'AK', "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", 'PR', "RI", "SC", 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'VI', 'WA', 'WV', 'WI', 'WY')))
#Creating an asthma prevalence variable
state_df <- state_df %>%
group_by(state, iyear) %>%
count(asthma_current) %>%
mutate(asthma_perc = (n/sum(n) * 100), asthma_perc = round(asthma_perc, 2)) %>%
filter(asthma_current == "yes")
#Creating hover label for map
state_df <- state_df %>%
mutate(hover = paste0(state, ": ", asthma_perc))
#Creating the state level asthma prevalence map in plotly
geo_properties = list(
scope = "usa",
projection = list(
type = "albers usa"),
showland = TRUE,
landcolor = toRGB('white'))
asthma_graph <- state_df %>%
plot_geo(
locationmode = 'USA-states',
frame = ~iyear) %>%
add_trace(locations = ~state,
z = ~asthma_perc,
zmin = 0,
zmax = 15,
text = ~hover,
hoverinfo = 'text',
color = ~asthma_perc,
colorscale = "Viridis") %>%
layout(geo = geo_properties,
font = "DM Sans",
title = "Asthma Prevalence in the United State, 2013-2021") %>%
colorbar(title = "Asthma Prevalence") %>%
animation_slider(
currentvalue = list(prefix = "YEAR "))
##Creating a map of asthma prevalence by urban county by year in plotly #First load the brfss_cities dataset from local brfss library
cities_df <- read_csv("brfss_data/brfss_cities.csv")
#create a prevalence variable for asthma_current and then make a hover label
cities_df <- cities_df %>%
group_by(fips, brfss_year, county, state) %>%
count(asthma_current) %>%
mutate(asthma_perc = (n/sum(n) * 100), asthma_perc = round(asthma_perc, 2)) %>%
filter(asthma_current == "yes") %>%
ungroup()
cities_df <- cities_df %>%
mutate(hover = paste0(county, ", ", state, ": ", asthma_perc, "%"),
nfips = sprintf("%05d", fips))
#Load county level map from the below link
url <- 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
counties <- rjson::fromJSON(file=url)
#Creating the map
g <- list(
scope = 'usa',
projection = list(type = 'albers usa'),
showlakes = TRUE,
lakecolor = toRGB('white'))
urban_graph <- cities_df %>%
plot_ly(
frame = ~brfss_year
) %>%
add_trace(
type="choropleth",
geojson=counties,
locations = cities_df$nfips,
z = cities_df$asthma_perc,
colorscale="Viridis",
zmin=0,
zmax=15,
text = ~hover,
hoverinfo = "text",
marker=list(line=list(
width=0))) %>%
layout(geo = g,
font = "DM Sans",
title = "Asthma Prevalence in US Urban Counties, 2013-2019") %>%
colorbar(title = "Asthma Prevalence") %>%
animation_slider(
currentvalue = list(prefix = "YEAR "))
urban_graph